package com.banshou.installs.ui.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.banshou.installs.R;
import com.banshou.installs.model.dao.FloorSet;
import com.banshou.installs.utils.ToastUtil;
import com.orhanobut.logger.Logger;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;
import java.util.Map;

/**
 * Created by cjq on 2018/4/22.
 * Email: stoic_yb@139.com
 * features:
 */
public class MMFloorSetAdapter extends CommonAdapter<FloorSet> {
    private Map<Integer, FloorSet> floorSetMap;
    private FloorSet mFloor;

    public MapListener mapListener;

    public interface MapListener {
        void setMap(Map<Integer, FloorSet> floorSetMap);
    }

    public void setMapListener(MapListener mapListener) {
        this.mapListener = mapListener;
    }

    public MMFloorSetAdapter(Context context, int layoutId, List<FloorSet> datas) {
        super(context, layoutId, datas);
        floorSetMap = new ArrayMap<>();
        for(int i = 0;i<datas.size();i++){
            floorSetMap.put(i,datas.get(i));
        }
    }

    @Override
    protected void convert(final ViewHolder holder, final FloorSet floorSet, final int position) {
        mFloor = null;
        holder.setTag(R.id.iv_check, new Integer(position));
//        if (floorSetMap.containsKey(position)) {
            mFloor = floorSetMap.get(position);
//        } else {
//            mFloor = floorSet;
//            floorSetMap.put(position, mFloor);
//        }
        final Button tv_left = holder.getView(R.id.tv_left);
        final Button tv_center = holder.getView(R.id.tv_center);
        final LinearLayout ll_right = holder.getView(R.id.right);
        final EditText et_right = holder.getView(R.id.et_input);

        String loucen = mFloor.getLoucen();
        holder.setText(R.id.tv_title, loucen.concat("楼"));
        tv_left.setText(loucen);
        tv_center.setVisibility(loucen.contains("-") ? View.VISIBLE : View.GONE);
        tv_center.setText(loucen.contains("-") ? loucen.replace("-", "B") : loucen);
        holder.setImageResource(R.id.iv_check, mFloor.isCommon() ? R.drawable.right : R.drawable.gary);
        if (mFloor.getName().equals(loucen)) {
            holder.setText(R.id.et_input, "");
            holder.setVisible(R.id.iv_cancel, true);
            setEnable(1, tv_left, tv_center, ll_right, et_right, "");
        } else if (mFloor.getName().contains("B")) {
            holder.setText(R.id.et_input, "");
            holder.setVisible(R.id.iv_cancel, true);
            setEnable(2, tv_left, tv_center, ll_right, et_right, "");
        } else {
            holder.setText(R.id.et_input, mFloor.getName());
            holder.setVisible(R.id.iv_cancel, false);
            setEnable(3, tv_left, tv_center, ll_right, et_right, mFloor.getName());
        }
        holder.setOnClickListener(R.id.iv_check, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FloorSet floorSet2 = floorSetMap.get(position);
                floorSet2.setCommon(!floorSet2.isCommon());
                mapListener.setMap(floorSetMap);
                holder.setImageResource(R.id.iv_check, floorSet2.isCommon() ? R.drawable.right : R.drawable.gary);
            }
        });

        tv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_right.clearFocus();
                holder.setText(R.id.et_input, "");
                holder.setVisible(R.id.iv_cancel, true);
                FloorSet floorSet2 = floorSetMap.get(position);
                floorSet2.setName(tv_left.getText().toString());
                setEnable(1, tv_left, tv_center, ll_right, et_right, "");
                mapListener.setMap(floorSetMap);
            }
        });

        tv_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_right.clearFocus();
                FloorSet floorSet2 = floorSetMap.get(position);
                floorSet2.setName(tv_center.getText().toString());
                holder.setText(R.id.et_input, "");
                holder.setVisible(R.id.iv_cancel, true);
                setEnable(2, tv_left, tv_center, ll_right, et_right, "");
                mapListener.setMap(floorSetMap);
            }
        });

        et_right.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    holder.setVisible(R.id.iv_cancel, false);
                    setEnable(3, tv_left, tv_center, ll_right, et_right, "");
                } else {
                    if (TextUtils.isEmpty(et_right.getText())) {
                        holder.setVisible(R.id.iv_cancel, true);
                    } else {
                        FloorSet floorSet2 = floorSetMap.get(position);
                        floorSet2.setName(et_right.getText().toString());
                        mapListener.setMap(floorSetMap);
                        holder.setVisible(R.id.iv_cancel, false);
                    }
                }
            }
        });

    }

    private void setEnable(int i, Button tv_left, Button tv_center, LinearLayout ll_right, EditText et_right, String name) {
        switch (i) {
            case 1:
                tv_left.setSelected(true);
                tv_center.setSelected(false);
                ll_right.setBackgroundResource(R.drawable.floor_set_unselect);
                break;
            case 2:
                tv_left.setSelected(false);
                tv_center.setSelected(true);
                ll_right.setBackgroundResource(R.drawable.floor_set_unselect);
                break;
            case 3:
                tv_left.setSelected(false);
                tv_center.setSelected(false);
                ll_right.setBackgroundResource(R.drawable.floor_set_select);
                break;
        }

    }
}
